`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    18:00:50 03/31/2014 
// Design Name: 
// Module Name:    RAM 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module RAM(data_out,//output
			  P1_Out, P1_Bullet, Alien_Pos, Alien_status, Bonus_Out, Score_Out, Shields_Out,
			  status,poll_line,vid_line,address,data_in,read,write,clk,Alien_Bullet); //input	  

////output [32:0] video_out;
//output [21:0] O_Player1;
//output [10:0] O_Score;
//output [362:0] O_Line1;
//output [362:0] O_Line2;
//output [362:0] O_Line3;
//output [362:0] O_Line4;
//output [362:0] O_Line5;
//output [43:0] O_Shield;
//output [32:0] O_Player1_bullet;
//output [32:0] O_RS_bullet;
//output [32:0] O_PS_bullet;
//output [32:0] O_SS_bullet;
//output [21:0] O_Bonus;


output [15:0] data_out;
output reg [12:0] P1_Out;  //[lives, X]
output reg [22:0] P1_Bullet; //[Active, X,Y]
output reg [21:0] Alien_Pos; //[X,Y]  is the false point
output reg [0:21] Alien_status; // [X:0] equals the one bit of alien statuses from RAM
output reg [11:0] Bonus_Out;  // [status, x]
output reg [10:0] Score_Out;
output reg [7:0] Shields_Out; //Each takes 2 bits
output reg [22:0] Alien_Bullet;

input [15:0] data_in;
input [5:0] status; //from keyboard
input [15:0] address;
input poll_line, read, write, vid_line, clk;
reg [15:0] RAM[255:0];

//reg [10:0] Player1[1:0]; //output for the VGA to display
//reg [10:0] Score;
//reg [32:0] Alien[54:0];
//reg [10:0] shield[3:0];
//reg [32:0] Player1_bullet;
//reg [32:0] RS_bullet;
//reg [32:0] PS_bullet;
//reg [32:0] SS_bullet;
//reg [10:0] Bonus[1:0];



assign data_out = RAM[address];

//assign video_out = {VRAM[2],VRAM[1],VRAM[0]};
//assign O_Player1 = {Player1[0], Player1[1]};
//assign O_Score = Score;
//assign O_Line1 = {Alien[0], Alien[1], Alien[2], Alien[3], Alien[4], Alien[5], Alien[6], Alien[7], Alien[8], Alien[9], Alien[10]};
//assign O_Line2 = {Alien[11], Alien[12], Alien[13], Alien[14], Alien[15], Alien[16], Alien[17], Alien[18], Alien[19], Alien[20], Alien[21]};
//assign O_Line3 = {Alien[22], Alien[23], Alien[24], Alien[25], Alien[26], Alien[27], Alien[28], Alien[29], Alien[30], Alien[31], Alien[32]};
//assign O_Line4 = {Alien[33], Alien[34], Alien[35], Alien[36], Alien[37], Alien[38], Alien[39], Alien[40], Alien[41], Alien[42], Alien[43]};
//assign O_Line5 = {Alien[44], Alien[45], Alien[46], Alien[47], Alien[48], Alien[49], Alien[50], Alien[51], Alien[52], Alien[53], Alien[54]};
//assign O_Shield = {shield[0], shield[1], shield[2], shield[3]};
//assign O_Player1_bullet = Player1_bullet;
//assign O_RS_bullet = RS_bullet;
//assign O_PS_bullet = PS_bullet;
//assign O_SS_bullet = SS_bullet;
//assign O_Bonus = {Bonus[0], Bonus[1]};




integer i;
initial
begin
for(i = 0; i<=255; i = i + 1)
RAM[i] <= 16'b0;
end

always @ (posedge clk)
	if(write) RAM[address] <= data_in;
	else if(poll_line)begin
		RAM[1] <= {14'd0, status[2:1]}; //p1 dir
		RAM[2] <= {15'd0, status[0]};	 //p1 bullet	
		RAM[3] <= {14'd0, status[5:4]}; //p2 dir
		RAM[4] <= {15'd0, status[3]};   //p2 bullet
	end
	else if(vid_line)begin
		P1_Out <= {RAM[9][1:0], RAM[8][10:0]};
		P1_Bullet <= {RAM[10][0], RAM[11][10:0], RAM[12][10:0]};
		Alien_Bullet <= {RAM[30][0], RAM[31][10:0], RAM[32][10:0]};
		Alien_Pos <= {RAM[33][10:0], RAM[34][10:0]};
		Bonus_Out <= {RAM[19][0], RAM[18][10:0]};
		Score_Out <= {RAM[5][10:0]};
		Shields_Out <= {RAM[20][1:0], RAM[21][1:0], RAM[22][1:0], RAM[23][1:0]}; // S1, S2, S3, S4
		Alien_status <= {RAM[35][0], RAM[38][0], RAM[41][0], RAM[44][0], RAM[47][0],
							  RAM[50][0], RAM[53][0], RAM[56][0], RAM[59][0], RAM[62][0],
							 RAM[65][0], RAM[68][0], RAM[71][0], RAM[74][0], RAM[77][0],
							 RAM[80][0], RAM[83][0], RAM[86][0], RAM[89][0], RAM[92][0],
							 RAM[95][0], RAM[98][0]}; /*RAM[101][0], RAM[104][0], RAM[107][0],
							 RAM[110][0], RAM[113][0], RAM[116][0], RAM[119][0], RAM[122][0],
							 RAM[125][0], RAM[128][0], RAM[131][0], RAM[134][0], RAM[137][0],
							 RAM[140][0], RAM[143][0], RAM[146][0], RAM[149][0], RAM[152][0],
							 RAM[155][0], RAM[158][0], RAM[161][0], RAM[164][0], RAM[167][0],
							 RAM[170][0], RAM[173][0], RAM[176][0], RAM[179][0], RAM[182][0],
							 RAM[185][0], RAM[188][0], RAM[191][0], RAM[194][0], RAM[197][0]}; */// status from top left to bottom right
	end


endmodule

// OLD VID PUSH
//		Player1[0] = RAM[10][10:0]; //player X
//		
//		Player1[1] = RAM[12][10:0]; //player lives
//		
//		Score = RAM[5][10:0];
//		
//		Alien[0]		 = {RAM[30][10:0], RAM[31][10:0], RAM[32][10:0]};
//		Alien[1]		 = {RAM[33][10:0], RAM[34][10:0], RAM[35][10:0]};
//		Alien[2]		 = {RAM[36][10:0], RAM[37][10:0], RAM[38][10:0]};
//		Alien[3]		 = {RAM[39][10:0], RAM[40][10:0], RAM[41][10:0]};
//		Alien[4]		 = {RAM[42][10:0], RAM[43][10:0], RAM[44][10:0]};
//		Alien[5]		 = {RAM[45][10:0], RAM[46][10:0], RAM[47][10:0]};
//		Alien[6]		 = {RAM[48][10:0], RAM[49][10:0], RAM[50][10:0]};
//		Alien[7]	 	 = {RAM[51][10:0], RAM[52][10:0], RAM[53][10:0]};
//		Alien[8]	 	 = {RAM[54][10:0], RAM[55][10:0], RAM[56][10:0]};
//		Alien[9]		 = {RAM[57][10:0], RAM[58][10:0], RAM[59][10:0]};
//		Alien[10]	 = {RAM[60][10:0], RAM[61][10:0], RAM[62][10:0]};
//		
//		
//		Alien[11]	 = {RAM[63][10:0], RAM[64][10:0], RAM[65][10:0]};
//		Alien[12]	 = {RAM[66][10:0], RAM[67][10:0], RAM[68][10:0]};
//		Alien[13]	 = {RAM[69][10:0], RAM[70][10:0], RAM[71][10:0]};
//		Alien[14]	 = {RAM[72][10:0], RAM[73][10:0], RAM[74][10:0]};
//		Alien[15]	 = {RAM[75][10:0], RAM[76][10:0], RAM[77][10:0]};
//		Alien[16]	 = {RAM[78][10:0], RAM[79][10:0], RAM[80][10:0]};
//		Alien[17]	 = {RAM[81][10:0], RAM[82][10:0], RAM[83][10:0]};
//		Alien[18]	 = {RAM[84][10:0], RAM[85][10:0], RAM[86][10:0]};
//		Alien[19]	 = {RAM[87][10:0], RAM[88][10:0], RAM[89][10:0]};
//		Alien[20] 	 = {RAM[90][10:0], RAM[91][10:0], RAM[92][10:0]};
//		Alien[21]	 = {RAM[93][10:0], RAM[94][10:0], RAM[95][10:0]};
//		
//		
//		Alien[22]	 = {RAM[96][10:0], RAM[97][10:0], RAM[98][10:0]};
//		Alien[23]	 = {RAM[99][10:0], RAM[100][10:0], RAM[101][10:0]};
//		Alien[24]	 = {RAM[102][10:0], RAM[103][10:0], RAM[104][10:0]};
//		Alien[25]	 = {RAM[105][10:0], RAM[106][10:0], RAM[107][10:0]};
//		Alien[26]	 = {RAM[108][10:0], RAM[109][10:0], RAM[110][10:0]};
//		Alien[27]	 = {RAM[111][10:0], RAM[112][10:0], RAM[113][10:0]};
//		Alien[28]	 = {RAM[114][10:0], RAM[115][10:0], RAM[116][10:0]};
//		Alien[29]	 = {RAM[117][10:0], RAM[118][10:0], RAM[119][10:0]};
//		Alien[30]    = {RAM[120][10:0], RAM[121][10:0], RAM[122][10:0]};
//		Alien[31]	 = {RAM[123][10:0], RAM[124][10:0], RAM[125][10:0]};
//		Alien[32]	 = {RAM[126][10:0], RAM[127][10:0], RAM[128][10:0]};
//		
//	   
//		Alien[33]	 = {RAM[129][10:0], RAM[130][10:0], RAM[131][10:0]};
//		Alien[34]	 = {RAM[132][10:0], RAM[133][10:0], RAM[134][10:0]};
//		Alien[35]	 = {RAM[135][10:0], RAM[136][10:0], RAM[137][10:0]};
//		Alien[36]	 = {RAM[138][10:0], RAM[139][10:0], RAM[140][10:0]};
//		Alien[37]	 = {RAM[141][10:0], RAM[142][10:0], RAM[143][10:0]};
//		Alien[38]	 = {RAM[144][10:0], RAM[145][10:0], RAM[146][10:0]};
//		Alien[39]	 = {RAM[147][10:0], RAM[148][10:0], RAM[149][10:0]};
//		Alien[40]    = {RAM[150][10:0], RAM[151][10:0], RAM[152][10:0]};
//		Alien[41]	 = {RAM[153][10:0], RAM[154][10:0], RAM[155][10:0]};
//		Alien[42]	 = {RAM[156][10:0], RAM[157][10:0], RAM[158][10:0]};
//		Alien[43]	 = {RAM[159][10:0], RAM[160][10:0], RAM[161][10:0]};
//		
//		
//		Alien[44]	 = {RAM[162][10:0], RAM[163][10:0], RAM[164][10:0]};
//		Alien[45]	 = {RAM[165][10:0], RAM[166][10:0], RAM[167][10:0]};
//		Alien[46]	 = {RAM[168][10:0], RAM[169][10:0], RAM[170][10:0]};
//		Alien[47]	 = {RAM[171][10:0], RAM[172][10:0], RAM[173][10:0]};
//		Alien[48]	 = {RAM[174][10:0], RAM[175][10:0], RAM[176][10:0]};
//		Alien[49]	 = {RAM[177][10:0], RAM[178][10:0], RAM[179][10:0]};
//		Alien[50]    = {RAM[180][10:0], RAM[181][10:0], RAM[182][10:0]};
//		Alien[51]	 = {RAM[183][10:0], RAM[184][10:0], RAM[185][10:0]};
//		Alien[52]	 = {RAM[186][10:0], RAM[187][10:0], RAM[188][10:0]};
//		Alien[53]	 = {RAM[189][10:0], RAM[190][10:0], RAM[191][10:0]};
//		Alien[54]	 = {RAM[192][10:0], RAM[193][10:0], RAM[194][10:0]};
//		
//		shield[0] 	 = RAM[206][10:0];
//		shield[1] 	 = RAM[207][10:0];
//		shield[2] 	 = RAM[208][10:0];
//		shield[3] 	 = RAM[209][10:0];
//		
//		Player1_bullet = {RAM[17][10:0], RAM[18][10:0], RAM[16][10:0]};
//		
//		RS_bullet	={RAM[196][10:0], RAM[197][10:0], RAM[195][10:0]};
//		PS_bullet	={RAM[199][10:0], RAM[200][10:0], RAM[198][10:0]};
//		SS_bullet	={RAM[202][10:0], RAM[203][10:0], RAM[201][10:0]};
//		
//		Bonus[0] 	= RAM[205][10:0];
//		Bonus[1] 	= RAM[204][10:0];